package com.wing.device.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.device.model.entity.Device;
import com.wing.device.model.entity.MaintenanceType;
import com.wing.device.service.DeviceMaintenanceTypeService;
import com.wing.web.db.config.util.DynamicDataSource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.1.0
 * @Author hz
 * @Date 2024-08-19 15:13
 **/
@Validated
@Api(value = "Admin-设备维修保养类型管理", tags = "#/device/maintenanceType")
@RestController("maintenanceTypeController")
@RequestMapping("device/admin/maintenanceType")
@Slf4j
public class DeviceMaintenanceTypeController {

    @Resource
    private DeviceMaintenanceTypeService deviceMaintenanceTypeService;

    @ApiOperation(value = "查询设备维修保养类型列表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<MaintenanceType> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            String name,
            String id
            ){

        Page<MaintenanceType> pg = new Page<>(page, limit);
        Map map = new HashMap();
        map.put("name",name);
        map.put("id",id);
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        map.put("databaseUser", databaseUser);
        IPage<MaintenanceType> list = deviceMaintenanceTypeService.pageList4app(pg, map);
        //关联相关业务属性
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询所有维保类型", notes = "查询")
    @GetMapping("/getAll")
    public JsonResult<MaintenanceType> getAll(String taskNature){
        QueryWrapper<MaintenanceType> wrapper = new QueryWrapper<>();
        wrapper.select("id","no","name");
        if (StrUtil.isNotEmpty(taskNature)){
            wrapper.eq("task_nature",Long.valueOf(taskNature));
        }
        List<MaintenanceType> maintenanceTypes = deviceMaintenanceTypeService.list(wrapper);
        return JsonResult.success(maintenanceTypes);
    }

    /**
     * 启用维保类型
     * @param id 维保id
     * @return
     */
    @ApiOperation(value = "启用", notes = "启用")
    @PostMapping("/enable")
    public JsonResult enable(Long id) {
        MaintenanceType maintenanceType = deviceMaintenanceTypeService.getById(id);
        if(maintenanceType == null){
            return JsonResult.fail("启用失败！");
        }
        maintenanceType.setIsDeleted(true);
        if (deviceMaintenanceTypeService.updateById(maintenanceType)) {
            return JsonResult.success("启用成功！");
        }
        return JsonResult.fail("启用失败！");
    }

    /**
     * 启用维保类型
     * @param id 维保id
     * @return
     */
    @ApiOperation(value = "启用", notes = "启用")
    @PostMapping("/disable")
    public JsonResult disable(Long id) {
        MaintenanceType maintenanceType = deviceMaintenanceTypeService.getById(id);
        if(maintenanceType == null){
            return JsonResult.fail("禁用失败！");
        }
        maintenanceType.setIsDeleted(false);
        if (deviceMaintenanceTypeService.updateById(maintenanceType)) {
            return JsonResult.success("禁用成功！");
        }
        return JsonResult.fail("禁失败！");
    }

    @ApiOperation(value = "添加维保类型", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody MaintenanceType maintenanceType) {
        try {
            if (StrUtil.isEmpty(maintenanceType.getNo())){
                return JsonResult.fail("类别编码必填!");
            }
            if (StrUtil.isEmpty(maintenanceType.getTaskNature())){
                return JsonResult.fail("类别性子必选!");
            }
            deviceMaintenanceTypeService.save(maintenanceType);
            return JsonResult.success("添加成功");
        } catch (Exception e) {
            log.error("添加失败:", e);
            return JsonResult.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "修改产品", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@RequestBody MaintenanceType maintenanceType) {
        try {
            if (null == maintenanceType.getId()){
                return JsonResult.fail("修改id为空!");
            }
            MaintenanceType type = deviceMaintenanceTypeService.getById(maintenanceType.getId());
            if(type == null){
                return JsonResult.fail("无效id！");
            }
            if (StrUtil.isEmpty(maintenanceType.getNo())){
                return JsonResult.fail("类别编码必填!");
            }
            deviceMaintenanceTypeService.updateById(maintenanceType);
            return JsonResult.success("修改成功！");
        } catch (Exception e) {
            return JsonResult.fail("修改失败:" + e.getMessage());
        }
    }
}
